package com.iqiyi.pps.epg.core.model.auth;

import com.google.common.collect.Lists;
import com.iqiyi.kiwi.utils.ConvertUtils;
import com.iqiyi.pps.epg.core.model.base.AuditableEntity;
import org.hibernate.annotations.*;
import org.hibernate.annotations.Cache;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import java.util.List;

/**
 * 角色.
 * 
 */
@Entity
@Table(name = "show_auth_role")
@Cache(usage = CacheConcurrencyStrategy.NONE)
public class Role extends AuditableEntity {

    private static final long serialVersionUID = 16102934369146485L;
    private String name;                // 角色名称
    private String description;         // 角色描述
    private Boolean enabled = false;    // 是否可用
	private List<Authority> authorityList = Lists.newArrayList();  //授权列表
    protected Long id;


	public void setId(Long id) {
		this.id = id;
	}
    @Override
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
		return id;
	}
	public Role() {

	}

	public Role(Long id, String name) {
		this.id = id;
		this.name = name;
	}

	@Column(nullable = false, unique = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    @ManyToMany(fetch = FetchType.EAGER)
    @Cascade( { org.hibernate.annotations.CascadeType.MERGE })
	@JoinTable(name = "show_auth_role_authority", joinColumns = { @JoinColumn(name = "ROLE_ID") }, inverseJoinColumns = { @JoinColumn(name = "AUTHORITY_ID") })
	@Fetch(FetchMode.SUBSELECT)
	@OrderBy("id")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Authority> getAuthorityList() {
		return authorityList;
	}

	public void setAuthorityList(List<Authority> authorityList) {
		this.authorityList = authorityList;
	}

	@Transient
	public String getAuthNames() {
		return ConvertUtils.convertElementPropertyToString(authorityList, "name", ", ");
	}

	@Transient
	@SuppressWarnings("unchecked")
	public List<Long> getAuthIds() {
		return ConvertUtils.convertElementPropertyToList(authorityList, "id");
	}
}
